package com.example.zhyq.sign

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.SearchView.OnQueryTextListener
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.example.base.entity.SignBean
import com.example.base.entity.SignBeanItem
import com.example.base.entity.VisitStatusBean
import com.example.base.mvvm.BaseActivity
import com.example.zhyq.R
import com.example.zhyq.databinding.ActivitySignListBinding
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.Calendar

/**
 *  考勤
 */

@AndroidEntryPoint
@Route(path = "/app/SignListActivity")
class SignListActivity : BaseActivity<ActivitySignListBinding,SignViewModel>() {


    val signStatusAdapter by lazy { SignStatusAdapter() }
    val signAdapter by lazy { SignAdapter() }
    lateinit var signs:List<SignBeanItem>//已经查出某月的集合
    var month=SimpleDateFormat("MM").format(System.currentTimeMillis())
    lateinit var signsByMonth:SignBean//根据月份查询的原集合

    val calendar = Calendar.getInstance()
    var clickNum=0
    override fun initView() {
        binding.headSign.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("考勤管理")
            headerAdd.visibility= View.GONE
        }

        val list = listOf<VisitStatusBean>(
            VisitStatusBean("全部", true),
            VisitStatusBean("正常", false),
            VisitStatusBean("迟到", false),
            VisitStatusBean("早退", false)
        )
        signStatusAdapter.addData(list)
        binding.signRvstatus.apply {
            adapter = signStatusAdapter
            layoutManager = LinearLayoutManager(this@SignListActivity,RecyclerView.HORIZONTAL,false)
        }

        signStatusAdapter.setOnItemClickListener { adapter, view, position ->
            signStatusAdapter.data.forEach {
                it.isCheck=false
            }
            signStatusAdapter.data[position].isCheck=true
            signStatusAdapter.notifyDataSetChanged()

            signAdapter.data.clear()
            if (position==0) {
                signAdapter.data.addAll(signs)
            }else {
                signAdapter.data.addAll(signs.filter {
                    it.status == signStatusAdapter.data[position].status
                })
            }
            signStatusAdapter.notifyDataSetChanged()
        }

        binding.rvSign.apply {
            adapter=signAdapter
            layoutManager=LinearLayoutManager(this@SignListActivity)
        }

        vm.getSigns(1066)

        binding.signSv.setOnQueryTextListener(object :OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                signAdapter.data.clear()
                signAdapter.addData(signs.filter {
                    if (it.morningCheckIn.isNullOrEmpty()){
                        it.afternoonCheckIn.split(" ")[0].contains(newText!!)
                    }else {
                        it.morningCheckIn.split(" ")[0].contains(newText!!)
                    }
                })
                signAdapter.notifyDataSetChanged()
                return true
            }

        })

        binding.signMonth.setText(month)

//        binding.signNext.setOnClickListener {
//            //clickNum++
//            selectByMonth(1)
//        }
//
//        binding.signPre.setOnClickListener {
//            //clickNum--
//            selectByMonth(-1)
//        }

    }

//    fun selectByMonth(clickNum:Int) {
//        calendar.add(Calendar.MONTH,clickNum)
//        val newMonth = SimpleDateFormat("MM").format(calendar.time)
//        binding.signMonth.setText(newMonth)
//
//        signAdapter.data.clear()
//        signs = signsByMonth.filter {
//            if (it.morningCheckIn.isNullOrEmpty()) {
//                it.afternoonCheckIn.split("-")[1]==newMonth
//            }else {
//                it.morningCheckIn.split("-")[1]==newMonth
//            }
//        }
//        signAdapter.addData(signs)
//        signAdapter.notifyDataSetChanged()
//    }

    override fun initData() {
        vm.getSigns.observe(this) {
            signsByMonth=it
            it.forEach {
                if (it.morningCheckIn.isNullOrEmpty()) {
                    it.type=1
                }else {
                    it.type=0
                }
            }
            signs=it.filter {
                if (it.morningCheckIn.isNullOrEmpty()) {
                    it.afternoonCheckIn.split("-")[1]==month
                }else {
                    it.morningCheckIn.split("-")[1]==month
                    //it.morningCheckIn.split(" ")[0].replaceFirst(Regex("[-/]"),"月")==month+"月"
                }
            }
            signAdapter.data.addAll(signs)
            signAdapter.notifyDataSetChanged()
        }
    }
}